﻿
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using lib.Model;
using lib.DAL;
using System.Linq;
namespace lib.BLL
{
    public partial class MenuCode_Bll
    {
        /// <summary>
        /// 保存菜单权限
        /// </summary>
        /// <param name="menuId">菜单ID</param>
        /// <param name="codes">菜单所需权限组合“,”隔开</param>
        /// <returns></returns>
        public bool SaveMenuCode(int menuId, string codes)
        {
            bool result = false;
            SqlConnection conn;
            using (SqlTransaction trans = SqlHelperWebDAL.CreateSqlTransaction(out conn))
            {
                try
                {
                    bool IsEx = false;
                    IList<MenuCode_Model> menuCodeModelList = MenuCode_Dal.Instance.GetModelList("fdMeCoMenuID=@fdMeCoMenuID", "", "", new SqlParameter("@fdMeCoMenuID", menuId));
                    if (menuCodeModelList != null && menuCodeModelList.Count > 0) { IsEx = true; }
                    string[] menuCode = codes.Split(',');
                    foreach (string str in menuCode)
                    {
                        if (!string.IsNullOrEmpty(str))
                        {
                            MenuCode_Model codeModel = new MenuCode_Model();
                            codeModel.FdMeCoMenuID = menuId;
                            if (IsEx)
                            {
                                var ImenuCode = menuCodeModelList.Where(m => m.FdMeCoMenuID == menuId && m.FdMeCoCode == int.Parse(str));
                                if (ImenuCode == null || ImenuCode.Count() == 0)
                                {
                                    codeModel.FdMeCoCode = int.Parse(str);
                                    MenuCode_Dal.Instance.Add(codeModel, trans);
                                }
                            }
                            else
                            {
                                codeModel.FdMeCoCode = int.Parse(str);
                                MenuCode_Dal.Instance.Add(codeModel, trans);
                            }
                        }
                    }
                    trans.Commit();
                    result = true;
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                }
                finally
                {
                    conn.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// 删除菜单权限
        /// </summary>
        /// <param name="menuId"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public bool DelMenuCode(int menuId, int code)
        {
            bool result = false;
            result = MenuCode_Dal.Instance.Delete("fdMeCoMenuID=@fdMeCoMenuID and fdMeCoCode=@fdMeCoCode", null,
                new SqlParameter("@fdMeCoMenuID", menuId), new SqlParameter("@fdMeCoCode", code)) > 0;
            return result;
        }
    }
}

